<template>
	<view class="classlist">
		<view class="loadingLayout" v-if="!classifyList.length && !noData">
			<uni-load-more status="loading"></uni-load-more>
		</view>
		<view class="content">
			<navigator :url="'/pages/preview/preview?id='+item._id" class="item" v-for="item in classifyList" :key="item._id">
				<image :src="item.smallPicurl" mode="aspectFill"></image>
			</navigator>
		</view>
		<view class="loadingLayout" v-if="classifyList.length || noData">
			<uni-load-more :status="noData?'noMore':'loading'"></uni-load-more>
		</view>
		<view class="safe-area-inset-bottom"></view>
	</view>
</template>
<script setup lang="ts">
	import { ref } from 'vue';
	import { apiGetClassifyList,apiGetMyScoreAndDownload } from '@/api/apis';
	import {onLoad, onUnload,onReachBottom, onShareAppMessage, onShareTimeline} from '@dcloudio/uni-app'
	import {gotoHome} from '@/utils/common.js'
	
	const classifyList = ref([])
	const noData = ref(false)
	let pageName;
	
	const queryParam = {
		pageNum:1,
		pageSize:12
	}
	
	/**
	 * 分享页面
	 */
	onShareAppMessage((e)=>{
		return {
			title:"咸虾米壁纸-"+pageName,
			path:"/pages/classlist/classlist?classid="+queryParam.classid+"&name="+pageName
		}
	})
	
	/**
	 * 分享朋友圈, 暂时不能用
	 */
	// onShareTimeline((e)=>{
	// 	return {
	// 		title:"咸虾米壁纸-"+pageName,
	// 		path:"classid="+queryParam.classid+"&name="+pageName
	// 	}
	// })
	
	const getClassifyList = async()=>{
		let res;
		if(queryParam.classid){
			res = await apiGetClassifyList(queryParam)	
		}
		
		if(queryParam.type){
			res= await apiGetMyScoreAndDownload(queryParam)
		}
		classifyList.value = [...classifyList.value, ...res.data]
		if(queryParam.pageSize > res.data.length){
			noData.value = true
		}
		//缓存数据
		uni.setStorageSync("storageClassList", classifyList.value)
	}
	
	/**
	 * 接收页面传递的参数
	 * 然后请求接口
	 */
	onLoad((options)=>{
		let {classid=null,id=null, name=null, type=null} = options
		if(type){
			queryParam.type = type
		}
		
		//id有值的时候, 赋值给classid
		if(id){
			classid = id
		}
		
		if(classid){
			queryParam.classid = classid
		}
		// if(!classid){
		// 	gotoHome()
		// 	return
		// }
		pageName = name
		uni.setNavigationBarTitle({
			title:name
		})
		getClassifyList()
	})
	
	onUnload(()=>{
		uni.removeStorageSync("storageClassList")
	})
	
	/**
	 * 触底触发
	 */
	onReachBottom(()=>{
		if(!noData.value){
			queryParam.pageNum++
			getClassifyList()			
		}
	})
	
</script>
<style scoped lang="scss">
	.classlist{
		.content{
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap:5rpx;
			padding: 5rpx;
			.item{
				height: 440rpx;
				image{
					width: 100%;
					height: 100%;
					display: block;
				}
			}
		}
	}
</style>
